package com.ws.service;

import com.github.pagehelper.PageHelper;
import com.ws.domain.Goods;
import com.ws.domain.User;
import com.ws.mapper.UserMapper;
import com.ws.util.SafeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    /**
     * 客户登录
     * @param username
     * @param password
     * @return
     */
    public User login(String username, String password) {
        return userMapper.login(username, SafeUtil.encode(password));
    }

    //查询所有客户
    public List<User> users() {
        return userMapper.users();
    }

    //添加客户
    public void userSave(User user) {
        //把添加的客户密码加密
        user.setPassword(SafeUtil.encode(user.getPassword()));
        userMapper.userSave(user);
    }


    //重置密码
    public void userReset(User user) {
        //重置密码前，加密客户密码
        user.setPassword(SafeUtil.encode(user.getPassword()));
        userMapper.userReset(user);
    }

    //根据ID跳转到修改客户信息页面
    public User userEdit(int id) {
        return userMapper.userEdit(id);
    }

   //修改客户信息
   public void userUpdate(User user) {
       userMapper.userUpdate(user);
   }

   //删除客户
   public void userDelete(int id) {
       userMapper.userDelete(id);
   }

    public List<User> pageUsers(int page, int size){
        PageHelper.startPage(page,4);
        return userMapper.users();
    }
    /**
     * 该方法用来分页查询所有客户信息时，获取客户的数量
     * @return 客户的数量
     */
    public int getCount() {
        return userMapper.getCount();
    }

    /**
     * 客户修改密码
     * @param user
     */
    public void passwordUpdate(User user) {
        userMapper.passwordUpdate(user);
    }


}
